package com.demo.hibernate.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.demo.hibernate.domain.HUserInformation;
import com.demo.hibernate.service.HLoginRegisterService;

@RestController
@RequestMapping("/hibernate")
public class HLoginRegisterController {

	@Autowired
	private HLoginRegisterService hLoginRegisterService;

	@RequestMapping("/login")
	public HUserInformation login(HUserInformation userInformation) {
		System.err.println("hibernate");
		System.err.println("参数：" + userInformation);
		HUserInformation result = hLoginRegisterService.login(userInformation);
		System.err.println("结果：" + result);
		return result;
	}

	@RequestMapping("/register")
	public HUserInformation register(HUserInformation userInformation) {
		System.err.println("hibernate");
		System.err.println("参数：" + userInformation);
		hLoginRegisterService.register(userInformation);
		System.err.println("结果：" + userInformation);
		return userInformation;
	}

	@RequestMapping("/list")
	public List<HUserInformation> list(Long start, Long end, Integer page) {
		System.err.println("hibernate");
		System.err.println("参数：" + start + "--" + end);
		List<HUserInformation> queryList = hLoginRegisterService.queryList(start, end);
		return queryList;
	}

	@RequestMapping("/listpage")
	public Page<HUserInformation> listPage(String search, Integer pageno) {
		System.err.println("hibernate");
		// 分页
		Sort sort = new Sort(Sort.Direction.DESC, "id");
		// pageno从0开始开始第一页
		Pageable pageable = new PageRequest(pageno, 5, sort);
		search = "%" + search + "%";
		Page<HUserInformation> pg = hLoginRegisterService.queryListPage(search, pageable);
		return pg;
	}

}
